﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NoteExpress;
using System.Threading;

namespace NEUT2
{
    public partial class uc查重 : UserControl,IContext
    {
        public uc查重()
        {
             ne= new NEHost2();
            InitializeComponent();
        }

        INEHost2 ne;
        INEFolder folder1, folder2, folder3;

        private void folder1_Click(object sender, EventArgs e)
        {
            folder1 = ne.Libraries.ActiveFolder;
            textBox1.Text = folder1.Title;
        }

        private void folder2_Click(object sender, EventArgs e)
        {
            folder2 = ne.Libraries.ActiveFolder;
            textBox2.Text = folder2.Title;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Label.Text = @"工作中……";

            var thread = new Thread(new ThreadStart(
                () =>
                {
                    //var sw = new System.Diagnostics.Stopwatch();
                    //sw.Start();

                    var dic1 = new Dictionary<string, INEItem>();
                    if (!checkBox1.Checked)
                        for (int i = 0; i < folder1.Items.Count; i++)
                        {
                            var tmp = folder1.Items.get_Items(i);
                            dic1.Add(tmp.UID, tmp);
                        }
                    else
                        FindItems(folder1, ref dic1);
                    var dic2 = new Dictionary<string, INEItem>();
                    if (!checkBox2.Checked)
                        for (int i = 0; i < folder2.Items.Count; i++)
                        {
                            var tmp = folder2.Items.get_Items(i);
                            dic2.Add(tmp.UID, tmp);
                        }
                    else
                        FindItems(folder2, ref dic2);
                    foreach (var t in dic1)
                    {
                        if (dic2.ContainsKey(t.Key))
                            if (!folder3.LinkItem(t.Value))
                                throw new Exception("Error");
                    }

                    Label.Text = @"工作完成！";

                    //sw.Stop();
                    //MessageBox.Show(sw.ElapsedMilliseconds.ToString());
                }));
            thread.Start();
        }

        void FindItems(INEFolder folder, ref Dictionary<string, INEItem> items)
        {
            for (int i = 0; i < folder.Items.Count; i++)
            {
                var tmp = folder.Items.get_Items(i);
                items[tmp.UID] = tmp;
            }
            for (int i = 0; i < folder.Count; i++)
            {
                FindItems(folder.get_Children(i), ref items);
            }
        }

        private void folder3_Click(object sender, EventArgs e)
        {
            folder3 = ne.Libraries.ActiveFolder;
            textBox3.Text = folder3.Title;
        }

        #region IContext Members

        public string MenuName
        {
            get { return "查重"; }
        }

        public string Tips
        {
            get { return "简介：查找两个文件夹的共有链接题录。"; }
        }

        #endregion

        #region IContext Members


        public ToolStripLabel Label
        {
            get;
            set;
        }

        #endregion
    }
}
